package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    @Override
    public List<SkuDTO> querySkuListBySpuId(Long id) {
        List<Sku> skuList = this.query().eq("spu_id", id).list();
        return SkuDTO.convertEntityList(skuList);
    }

    @Override
    public List<SkuDTO> querySkuListByIds(List<Long> ids) {
        return SkuDTO.convertEntityList(this.listByIds(ids));
    }

    @Override
    @Transactional
    public void deductStock(Map<Long, Integer> cartMap) {

        //批量扣减库存
        executeBatch(sqlSession->{
            Set<Map.Entry<Long, Integer>> entries = cartMap.entrySet();
            for (Map.Entry<Long, Integer> entry : entries) {
                // 如果充足，扣减库存 update tb_sku set stock = stock - 1, sold = sold + 1  where id = 1
                // 准备参数
                Map<String,Object> param = new HashMap<>();
                param.put("id", entry.getKey());
                param.put("num", entry.getValue());
                // 编译statement，namespace.statementId
                sqlSession.update("com.leyou.item.mapper.SkuMapper.deductStock",param);
            }
            //一次性刷入所有的statement达到批量执行的目的
            sqlSession.flushStatements();
        });

//        executeBatch(sqlSession -> {
//            //循环想sqlSession中添加statement，myBatis，map传参
//            cartsMap.entrySet().forEach(cart->{
//
//                Map<String,Object> paramMap = new HashMap<>();
//                paramMap.put("skuId",cart.getKey());
//                paramMap.put("num",cart.getValue());
//
//                sqlSession.update("com.leyou.item.mapper.SkuMapper.minusStock",paramMap);
//
//            });
//
//            //一次性刷入所有的statement达到批量执行的目的
//            sqlSession.flushStatements();
//
//        });

//        Set<Map.Entry<Long, Integer>> entries = cartMap.entrySet();
//        for (Map.Entry<Long, Integer> entry : entries) {
//            Long skuId = entry.getKey();
//            Integer num = entry.getValue();
//            // 查询sku
//            Sku sku = getById(skuId);
//            //  判断库存是否充足
//            if (sku.getStock() < num) {
//                // 如果不足，抛出异常
//                throw new LyException(400, "库存不足！");
//            }
//
//            // 如果充足，扣减库存 update tb_sku set stock = stock - 1, sold = sold + 1  where id = 1
//            Map<String,Object> param = new HashMap<>();
//            param.put("id", skuId);
//            param.put("num", num);
//            this.baseMapper.deductStock(param);
//        }
    }

    @Override
    @Transactional
    public void addStock(Map<Long, Integer> cartMap) {

        executeBatch(sqlSession -> {
            for (Map.Entry<Long, Integer> entry : cartMap.entrySet()) {
                Map<String,Object> param = new HashMap<>();
                param.put("id", entry.getKey());
                param.put("num", entry.getValue());
                sqlSession.update("com.leyou.item.mapper.SkuMapper.addStock", param);
            }
            //一次性刷入所有的statement达到批量执行的目的
            sqlSession.flushStatements();
        });
    }
}
